window.PIXI = require('libs/pixi.min.js')
window.p2 = require('libs/p2.min.js')
window.Phaser = require('libs/phaser-split.min.js')
window.scrollTo = function () { }




let game
let sprite

/**
 * 尺寸
 * @type {{w: number, h: number}}
 */
let { screenWidth, screenHeight } = wx.getSystemInfoSync();
let flappy_font;

let size = { w: screenWidth, h: screenHeight }

let screenSize = { w: screenWidth, h: screenHeight }
let spriteSize = { w: 24, h: 24 }

var sprite1;
var sprite2;
var sprite3;
var sprite4;
var sprite5;
var sprite6;

var dp;

var debugRect;
var overlapRect;

var mpxi = function (x) { return x };


/**
 * 游戏主函数
 */
export default class Main {

  /**
   * 构造
   */
  constructor(_width, _height) {
    // 配置参数
    console.log(_width);
    console.log(_height);
    //分辨率自适应
    let canvasRatio = canvas.width / canvas.height;
    let gameRatio = _width / _height;
    const conf = {
      width: canvasRatio<gameRatio? _width: (_height * canvasRatio),
      height: canvasRatio > gameRatio ? _height : (_width / canvasRatio),
      canvas: canvas,
      //context: canvas.getContext('webgl',  { alpha: false, depth: true, stencil: true, antialias: true, premultipliedAlpha: false, preserveDrawingBuffer: true }),
      renderer:Phaser.Auto,
      parent: 'phaser',
      transparent: false,
      antialias: false,
      scaleMode: Phaser.ScaleManager.EXACT_FIT
    };

    // 创建游戏
    game = new Phaser.Game(conf)
    game.States = {}; //存放state对象

    // 获取尺寸
    screenSize.w = window.innerWidth
    screenSize.h = window.innerHeight
    console.log('屏幕尺寸: ', screenSize.w, 'x', screenSize.h);
    game.States.boot = function () {
      this.preload = function () {
        game.innerWidth = _width;
        game.innerHeight = _height;
        game.offsetLeft = (game.width - game.innerWidth) / 2;
        game.offsetTop = (game.height - game.innerHeight) / 2;
        game.resolutionScale = game.innerWidth / window.innerWidth;
        if (!game.device.desktop) {
          this.scale.scaleMode = Phaser.ScaleManager.EXACT_FIT;
          this.scale.forcePortrait = true;
          this.scale.refresh();
          console.log('refresh')
        }
        game.load.image('loading', 'assets/preloader.gif');
      };
      this.create = function () {
        game.state.start('preload'); //跳转到资源加载页面
      };
    }

    game.States.preload = function () {
      this.preload = function () {
        var preloadSprite = game.add.sprite(35, game.height / 2, 'loading'); //创建显示loading进度的sprite
        game.load.setPreloadSprite(preloadSprite);
        //以下为要加载的资源
        game.load.image('background', 'assets/sky.png'); //背景

        game.load.spritesheet('qiu', 'assets/qiu.png', 35, 50, 10); //球
        game.load.image('fk', 'assets/fk.png', 35, 35); //球
        game.load.image('dp', 'assets/dp.png');

      
      }
      this.create = function () {
        game.state.start('menu');
      }
    }

    game.States.menu = function () {
      this.create = function () {
        console.log(game.width);
        game.add.tileSprite(0, 0, game.width, game.height, 'background').autoScroll(0, 30); //背景图

        game.physics.startSystem(Phaser.Physics.P2JS);

        mpxi = game.physics.p2.mpxi;

        //  Make things a bit more bouncey
        game.physics.p2.defaultRestitution = 0.8;

        game.physics.p2.gravity.y = 300;

        var spriteMaterial = game.physics.p2.createMaterial('spriteMaterial');
        var qiqiuMaterial = game.physics.p2.createMaterial('qiqiuMaterial');

        var contactMaterial = game.physics.p2.createContactMaterial(spriteMaterial, qiqiuMaterial, { restitution: 0.1 });

        //game.physics.p2.setWorldMaterial(qiqiuMaterial);

        sprite1 = game.add.sprite(200, 100, 'fk');
        sprite2 = game.add.sprite(245, 100, 'fk');
        sprite3 = game.add.sprite(290, 100, 'fk');
        sprite4 = game.add.sprite(335, 100, 'fk');
        sprite5 = game.add.sprite(380, 100, 'fk');
        sprite6 = game.add.sprite(425, 100, 'fk');


        dp = game.add.sprite((game.width - 34) / 2, game.height - 250, 'dp');

        var qiqiu = game.add.sprite((game.width - 34) / 2, game.height - 150, 'qiu'); //球

        game.physics.p2.enable([sprite1, sprite2, sprite3, sprite4, sprite5,sprite6,dp]);

        dp.body.setCircle(50);


        //  Modify a few body properties
        sprite1.body.setZeroDamping();
        sprite1.body.fixedRotation = true;
        game.physics.p2.enable([qiqiu]);

        sprite1.body.setMaterial(spriteMaterial);
        sprite2.body.setMaterial(spriteMaterial);
        sprite3.body.setMaterial(spriteMaterial);
        sprite4.body.setMaterial(spriteMaterial);
        sprite5.body.setMaterial(spriteMaterial);
        sprite6.body.setMaterial(spriteMaterial);

        dp.body.setMaterial(qiqiuMaterial);
        dp.smoothed = false;
        dp.body.static = true;
        dp.body.data.gravityScale = 0;
        qiqiu.body.static = true;
        qiqiu.body.setMaterial(qiqiuMaterial);

        sprite1.body.data.gravityScale = 0.25;
        sprite2.body.data.gravityScale = 0.25;
        sprite3.body.data.gravityScale = 0.25;
        sprite4.body.data.gravityScale = 0.25;
        sprite5.body.data.gravityScale = 0.25;
        sprite6.body.data.gravityScale = 0.25;
        qiqiu.body.data.gravityScale = 0;

        debugRect = new Phaser.Rectangle();
        overlapRect = new Phaser.Rectangle(0, game.height - 100, game.width, 100);

        game.input.addMoveCallback(function (pointer, x, y, isDown){
          dp.body.x=x;
          dp.body.y=y;
        }, this);






        

 

        

      
      
      },
      this.render=function(){
        var bodyAABB = sprite1.body.data.aabb;

        debugRect.x = mpxi(bodyAABB.upperBound[0]);
        debugRect.y = mpxi(bodyAABB.upperBound[1]);
        debugRect.right = mpxi(bodyAABB.lowerBound[0]);
        debugRect.bottom = mpxi(bodyAABB.lowerBound[1]);


        game.debug.geom(debugRect);
        game.debug.geom(overlapRect);


      },
      this.update = function () {
        if (debugRect.intersects(overlapRect)) {
         // console.log('Overlapping');

        }
        else {
          //console.log('moving');
        }
      }
    }



    game.state.add('boot', game.States.boot);
    game.state.add('preload', game.States.preload);
    game.state.add('menu', game.States.menu);
    game.state.start('boot'); //启动游戏
  }

}